/**
 * 采购申请purchaseApply.js
 * 版本号： v1.0.0
 */
$(function () {

    /**
     * 时间选中事件
     */
    $("#startDate").datebox({
        onSelect : function(beginDate){
            $('#endDate').datebox().datebox('calendar').calendar({
                validator: function(date){
                    return beginDate<=date;
                }
            });
        }
    });

    var lastChecked = null;
    $("#purchaseApply_dataList").datagrid({
        fitColumns: true,
        fit: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/purchaseApply/purchaseApplyList.do',
        idField: 'purId',
        frozenColumns: [[
            {field: 'purId', checkbox: true},
            {field: 'cdId', checkbox: true,hidden:true},
            {field: 'pfId', checkbox: true,hidden:true}
        ]],
        columns: [
            [
                {field: 'prName', title: '项目名称', width: 160, fixed: true},
                {field: 'coName', title: '合同名称', width: 160, fixed: true},
                {field: 'coNumber', title: '合同编号', width: 140, fixed: true},
                {field: 'coCode', title: '合同单号', width: 125, fixed: true},
                {field: 'purCode', title: '采购单号', width: 165, fixed: true},
                {field: 'checkState', title: '审核状态', width: 60, fixed:true, align:'center', formatter:function (val) {
                    if (val == 0){
                        return "<span style='color: #2c24ff; font-weight: bold'>未审核</span>";
                    }
                    if (val == 1){
                        return "<span style='color: deepskyblue; font-weight: bold'>审核中</span>";
                    }
                    if (val == 2){
                        return "<span style='color: green; font-weight: bold'>已通过</span>";
                    }
                    if (val == 3){
                        return "<span style='color: red; font-weight: bold'>已驳回</span>";
                    }
                }},
                {field: 'createDate', title: '填写日期', width: 75, fixed:true, align:'center'},
                {field: 'purType', title: '请购类别', width: 60, fixed:true, align:'center', formatter: function (val) {
                        if (val == "1") {
                            return "合同内";
                        }
                        if (val == "2") {
                            return "合同外";
                        }
                }},
                {field: 'reportState', title: '上报状态', width: 60, fixed:true, align:'center', formatter:function (val) {
                    if (val == "1"){
                        return "<span style='color: red; font-weight: bold'>未上报</span>";
                    }
                    if (val == "2"){
                        return "<span style='color: green; font-weight: bold'>已上报</span>";
                    }
                }},
                {field: 'detailStatus', title: '清单状态', width: 60, align:'center', formatter:function(val){
                        if(val !="0"){
                            return "已录入";
                        }else{
                            return "未录入";
                        }
                }},
                {field: 'purPerson', title: '请购人', width: 80, fixed:true}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#purchaseApply_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#purchaseApply_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#purchaseApply_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#purchaseApply_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //上报按钮
                if("applyBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.purId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            changeReportState();
                        }
                    });
                }
                //添加按钮
                if("addBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.purId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            window.location.href = CONTEXT_PATH + "/purchaseApply/toPurchaseApply.do";
                        }
                    });
                }
                //修改按钮
                if("editBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.purId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            purchaseApply_edit();
                        }
                    });
                }
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.purId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            purchaseApply_findList();
                        }
                    });
                }
                //采购清单编辑按钮
                if("detailEditBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.purId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            toPurchaseDetailEdit();
                        }
                    });
                }
                //删除按钮
                if("delBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.purId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            purchaseApply_del();
                        }
                    });
                }
                //查看详细按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.purId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            purchaseApply_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

//查询采购申请
function purchaseApply_findList() {
    refreshPageNumber("purchaseApply_dataList");
    $("#purchaseApply_dataList").datagrid("reload",{
        prName:$("#prName").val(),
        coNumber:$("#coNumber").val(),
        startDate:$("#startDate").datebox("getValue"),
        endDate:$("#endDate").datebox("getValue"),
        coName:$("#coName").val(),
        purType:$("#purType").combobox("getValue"),
        reportState:$("#reportState").combobox("getValue")
    });
    //清楚选中项
    $("#purchaseApply_dataList").datagrid("clearSelections");
}

/***
 * 动态刷新页面
 * @param coId
 */
function refreshPageNumber(coId) {
    //获取dataGrid的列表对象属性
    var $datagrid = $("#" + coId).datagrid("options");
    if ($datagrid != undefined) {
        $datagrid.pageNumber = 1;
    }
    //获取dataGrid的分页对象
    var $getPager = $("#" + coId).datagrid('getPager');
    var $pagination = $($getPager).pagination("options");
    if ($pagination != undefined) {
        $pagination.pageNumber = 1;
    }
}

/**
 * 查看采购申请详情
 */
function purchaseApply_view(iconCls){
    var row = $("#purchaseApply_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的采购申请详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(cgsq.id,cgsq.url + row.purId+"&cdId="+row.cdId+"&pfId="+row.pfId, iconCls, cgsq.closable,cgsq.title);
}

/**
 * 采购清单编辑
 */
function toPurchaseDetailEdit(iconCls){
    var row = $("#purchaseApply_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要编辑的采购申请", timeout: 3000, showType: "slide" });
        return;
    }
    //判断申请状态，若已上报的采购，不能再编辑清单
    if (row.reportState != 1){
        $.messager.show({ title: '操作提示', msg: "只能对未上报的申请编辑采购清单", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(cgqdbj.id,cgqdbj.url + row.purId, iconCls, cgqdbj.closable,cgqdbj.title);
}

/**
 * 删除项目
 */
function purchaseApply_del() {
    var row = $("#purchaseApply_dataList").datagrid("getSelected");
    if(row.reportState!=2) {
        var row = $("#purchaseApply_dataList").datagrid("getSelected");
        var checked = $("#purchaseApply_dataList").datagrid("getChecked");

        if (!checked || !checked.length) {
            $.messager.show({title: '操作提示', msg: "请选择删除采购申请单", timeout: 3000, showType: "slide"});
            return;
        }
        $.messager.confirm('删除提示', '您确定要删除这条采购申请单吗？', function (r) {
            if (r) {
                $.ajax({
                    url: CONTEXT_PATH + "/purchaseApply/del.do?purId=" + row.purId,
                    type: "get",
                    success: function (data) {
                        messager(data);
                        $("#purchaseApply_dataList").datagrid("reload", {
                            prName:$("#prName").val(),
                            coNumber:$("#coNumber").val(),
                            startDate:$("#startDate").datetimebox("getValue"),
                            endDate:$("#endDate").datetimebox("getValue"),
                            coName:$("#coName").val(),
                            purType:$("#purType").combobox("getValue"),
                            reportState:$("#reportState").combobox("getValue")
                        });
                    }
                })
            }
        });
    }else{
        $.messager.show({title: '操作提示', msg: "已上报，不可删除！", timeout: 3000, showType: "slide"});
    }
}

/**
 * 修改上报状态
 */
function changeReportState() {
    var row = $("#purchaseApply_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要修改上报状态的对象", timeout: 3000, showType: "slide" });
        return;
    }
    if(row.detailStatus != '0'){
        $.messager.confirm('上报提示', '您确定要上报这条采购申请单吗？', function (r) {
            if(row.reportState!="2"){
                if (r) {
                    $.ajax({
                        url: CONTEXT_PATH + "/purchaseApply/changsReportState.do?",
                        data:{
                            "purId":row.purId,
                            "checkState":row.checkState
                        },
                        type: "get",
                        success: function (data) {
                            messager(data);
                            $("#purchaseApply_dataList").datagrid("reload", {
                                prName:$("#prName").val(),
                                coNumber:$("#coNumber").val(),
                                startDate:$("#startDate").datetimebox("getValue"),
                                endDate:$("#endDate").datetimebox("getValue"),
                                coName:$("#coName").val(),
                                purType:$("#purType").combobox("getValue"),
                                reportState:$("#reportState").combobox("getValue")
                            });
                        }
                    })
                }
            }else{
                $.messager.show({ title: '操作提示', msg: "请勿重复上报", timeout: 3000, showType: "slide" });
            }
        });
    }else{
        $.messager.show({ title: '操作提示', msg: "请填写请购清单后再上报！", timeout: 3000, showType: "slide" });
    }

}

/**
 * 修改采购申请
 * @returns {boolean}
 */
function purchaseApply_edit() {
    var row = $("#purchaseApply_dataList").datagrid("getSelected");
    if(row.reportState!=2){
        var row = $("#purchaseApply_dataList").datagrid("getSelected");
        var checked = $("#purchaseApply_dataList").datagrid("getChecked");
        if (!checked || !checked.length) {
            $.messager.show({title: '操作提示', msg: "请选择修改采购申请", timeout: 3000, showType: "slide"});
            return;
        }
        window.location.href = CONTEXT_PATH + "/purchaseApply/toPurchaseApply.do?purId=" + row.purId;
    }else{
        $.messager.show({title: '操作提示', msg: "已上报，不能修改！", timeout: 3000, showType: "slide"});
    }
}